/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;


import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.User;
import models.UserForm;
import models.UserValidator;

@WebServlet(name = "ValidateController", urlPatterns = { 
        "/wijzig", "/nieuw" })
public class ValidateController extends HttpServlet {
    
    private static final long serialVersionUID = 98279L;

    @Override
    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
            throws IOException, ServletException {
        process(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, 
            HttpServletResponse response)
            throws IOException, ServletException {
        process(request, response);
    }

    private void process(HttpServletRequest request,
            HttpServletResponse response) 
            throws IOException, ServletException {

        String uri = request.getRequestURI();
       
        int lastIndex = uri.lastIndexOf("/");
        String action = uri.substring(lastIndex + 1); 
        String dispatchUrl = null;

        if (action.equals("product_input")) {
            // no action class, there is nothing to be done
            dispatchUrl = "/ProductForm.jsp";
        } else if (action.equals("nieuw")) {
            // instantiate action class
            UserForm userform = new UserForm();
            // populate action properties
            userform.setName(request.getParameter("naam"));
            userform.setStreetAddress(request.getParameter("straatnaam"));
            userform.setCity(request.getParameter("plaats"));
            userform.setHuisnummer(request.getParameter("huisnummer"));
            
            // validate ProductForm
            UserValidator productValidator = new UserValidator();
            List<String> errors = productValidator.validate(userform);
            if (errors.isEmpty()) {
            	// create Product from ProductForm
            	User user = new User();
            	user.setName(userform.getName());
            	user.setCity(userform.getCity());
            	user.setStreetAddress(userform.getStreetAddress());
                user.setHuisnummer(userform.getHuisnummer());
            	
                // no validation error, execute action method
               
                // store action in a scope variable for the view
                request.setAttribute("product", user);
                dispatchUrl = "/gebruikers.jsp";
            } else {
                request.setAttribute("errors", errors);
                request.setAttribute("form", userform);
                dispatchUrl = "/gebruiker_wijzigen.jsp";
            }
        }

        // forward to a view
        if (dispatchUrl != null) {
            RequestDispatcher rd = 
                    request.getRequestDispatcher(dispatchUrl);
            rd.forward(request, response);
        }
    }
}
